using System;
using System.Collections.Generic;
using System.Text;
using FireStarter.Base;
using Firestarter.DB.Schema.BO;
using FireStarterModeller.BO;
using Firestarter.DB.Schema.Logic;
using Habanero.Base;
using NUnit.Framework;

namespace Firestarter.DB.Schema.Test.Logic
{
    [TestFixture]
    public class TestChangeTypeTreeInspector : TestBase
    {
        [Test]
        public void Test_GetCombinedChangeType_Simple()
        {
            //---------------Set up test pack-------------------
            Dictionary<IBusinessObject, ChangeType> allPendingChangeTypes = new Dictionary<IBusinessObject, ChangeType>();
            allPendingChangeTypes.Add(new DBTable(), ChangeType.None);
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            ChangeType combinedChangeType = ChangeTypeTreeInspector.GetCombinedChangeType(allPendingChangeTypes);
            //---------------Test Result -----------------------
            Assert.AreEqual(ChangeType.None, combinedChangeType);
        }

        [Test]
        public void Test_GetCombinedChangeType_Simple_Variant()
        {
            //---------------Set up test pack-------------------
            Dictionary<IBusinessObject, ChangeType> allPendingChangeTypes = new Dictionary<IBusinessObject, ChangeType>();
            allPendingChangeTypes.Add(new DBTable(), ChangeType.Add);
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            ChangeType combinedChangeType = ChangeTypeTreeInspector.GetCombinedChangeType(allPendingChangeTypes);
            //---------------Test Result -----------------------
            Assert.AreEqual(ChangeType.Add, combinedChangeType);
        }

        [Test]
        public void Test_GetCombinedChangeType_Multiple()
        {
            //---------------Set up test pack-------------------
            Dictionary<IBusinessObject, ChangeType> allPendingChangeTypes = new Dictionary<IBusinessObject, ChangeType>();
            allPendingChangeTypes.Add(new DBTable(), ChangeType.Add);
            allPendingChangeTypes.Add(new DBTable(), ChangeType.Update);
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            ChangeType combinedChangeType = ChangeTypeTreeInspector.GetCombinedChangeType(allPendingChangeTypes);
            //---------------Test Result -----------------------
            Assert.AreEqual(ChangeType.Add | ChangeType.Update, combinedChangeType);
        }

        [Test]
        public void Test_GetCombinedChangeType_Advanced()
        {
            //---------------Set up test pack-------------------
            Dictionary<IBusinessObject, ChangeType> allPendingChangeTypes = new Dictionary<IBusinessObject, ChangeType>();
            allPendingChangeTypes.Add(new DBTable(), ChangeType.Add);
            allPendingChangeTypes.Add(new DBTable(), ChangeType.Update | ChangeType.None);
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            ChangeType combinedChangeType = ChangeTypeTreeInspector.GetCombinedChangeType(allPendingChangeTypes);
            //---------------Test Result -----------------------
            Assert.AreEqual(ChangeType.Add | ChangeType.Update | ChangeType.None, combinedChangeType);
        }

        [Test]
        public void Test_GetAllPendingChangeTypes_NullObject()
        {
            //---------------Set up test pack-------------------
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            Dictionary<IBusinessObject, ChangeType> allPendingChangeTypes = 
                ChangeTypeTreeInspector.GetAllPendingChangeTypes(null);
            //---------------Test Result -----------------------
            Assert.AreEqual(0, allPendingChangeTypes.Count);
        }

        [Test]
        public void Test_GetAllPendingChangeTypes_OneLevel()
        {
            //---------------Set up test pack-------------------
            IBusinessObject businessObject = new DBDatabase() { PendingChangeType = ChangeType.None };
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            Dictionary<IBusinessObject, ChangeType> allPendingChangeTypes = ChangeTypeTreeInspector.GetAllPendingChangeTypes(businessObject);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, allPendingChangeTypes.Count);
            Assert.AreEqual(ChangeType.None, allPendingChangeTypes[businessObject]);
        }

        [Test]
        public void Test_GetAllPendingChangeTypes_OneLevel_Updated()
        {
            //---------------Set up test pack-------------------
            DBDatabase dbDatabase = new DBDatabase() { PendingChangeType = ChangeType.Update };
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            Dictionary<IBusinessObject, ChangeType> allPendingChangeTypes = ChangeTypeTreeInspector.GetAllPendingChangeTypes(dbDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, allPendingChangeTypes.Count);
            Assert.AreEqual(ChangeType.Update, allPendingChangeTypes[dbDatabase]);
        }

        [Test]
        public void Test_GetAllPendingChangeTypes_TwoLevels_Updated()
        {
            //---------------Set up test pack-------------------
            DBDatabase dbDatabase = new DBDatabase() { PendingChangeType = ChangeType.Update };
            DBTable dbTable = new DBTable() { PendingChangeType = ChangeType.Update };
            dbDatabase.Tables.Add(dbTable);
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            Dictionary<IBusinessObject, ChangeType> allPendingChangeTypes = ChangeTypeTreeInspector.GetAllPendingChangeTypes(dbDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(2, allPendingChangeTypes.Count);
            Assert.AreEqual(ChangeType.Update, allPendingChangeTypes[dbDatabase]);
            Assert.AreEqual(ChangeType.Update, allPendingChangeTypes[dbTable]);
        }

        [Test]
        public void Test_GetAllPendingChangeTypes_TwoLevels_Mixed()
        {
            //---------------Set up test pack-------------------
            DBDatabase dbDatabase = new DBDatabase() { PendingChangeType = ChangeType.Update };
            DBTable dbTable = new DBTable() { PendingChangeType = ChangeType.Add };
            dbDatabase.Tables.Add(dbTable);
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            Dictionary<IBusinessObject, ChangeType> allPendingChangeTypes = ChangeTypeTreeInspector.GetAllPendingChangeTypes(dbDatabase);
            //---------------Test Result -----------------------
            Assert.AreEqual(2, allPendingChangeTypes.Count);
            Assert.AreEqual(ChangeType.Update | ChangeType.Add, allPendingChangeTypes[dbDatabase]);
            Assert.AreEqual(ChangeType.Add, allPendingChangeTypes[dbTable]);
        }
    }
}
